<?php

namespace App\Models;

use Illuminate\Support\Collection;

class CarProperty extends Model
{
    protected $table = 'yf_car_property';
    protected $primaryKey = 'car_property_id';
    public $timestamps = false;
    protected $guarded = ['car_property_id'];

    public function carPropertyValues()
    {
        return $this->hasMany(CarPropertyValue::class, 'car_property_id', 'car_property_id');
    }

    public static function getPropertyAndValue(GoodsCat $goodsCat) : Collection
    {
        $catIds = $goodsCat->getAllChildrenIds();
        array_push($catIds, $goodsCat->cat_id);

        return self::where('car_property_recommend', 1)
            ->whereIn('car_cat_id', $catIds)
            ->whereIn('car_property_format', ['checkbox','radio','select'])
            ->with(['carPropertyValues' => function($query) {
                return $query->orderBy('car_property_value', 'ASC');
            }])
            ->get();
    }
}
